import { getUserInfoDetail,exitLogin } from "../../../api/login"

Component({
  data:{
    userInfoObj:{},
    list:[
      {title:"昵称",props:"nickName"},
      {title:"姓名",props:"userName"},
      {title:"性别",props:"sex"},
      {title:"身份证号",props:"idcard"},
      {title:"出生日期",props:"birthday"},
      {title:"联系方式",props:"phonenumber"},
      {title:"家庭住址",props:"address"},
      {title:"邀请码",props:"inviteCode"},
      {title:"校区",props:"schoolId"}
    ]
  },
  methods:{
    onShow(){
    this.getUserInfoObj()
    },
   async getUserInfoObj(){
     const {userId} = getApp().getUserInfo()
    const {code,data:res} = await getUserInfoDetail({
      userId
    })
    if(code === 200){
     const modelData = JSON.parse(JSON.stringify(res))
     const getSchoolList = getApp().getSchoolList()
     modelData.sex =  modelData.sex === '0' ? '男' : '女'
     modelData.schoolId = getSchoolList.filter((val:any)=>modelData.schoolId == val.id)[0]?.name || ""
     this.setData({
      userInfoObj:res,
      modelData
      }) 
    }

   },
  //  修改设置信息
  alterSetting(){
    const userInfoObj = JSON.stringify(this.data.userInfoObj)
    wx.navigateTo({
      url:`/pages/register/register?userInfoObj=${encodeURIComponent(userInfoObj)}`
    })
  },
//  退出登录
exitLogin(){
exitLogin().then(async res=>{
  if(res.code === 200){
    // 清除用户端本地存储
    await getApp().removeToken("token")
    let storageList =  ['userInfo','schoolList','memberList','schoolId']
    storageList.forEach((val:string)=>{
      wx.removeStorageSync(val)
    })

    wx.navigateTo({
      url:"/pages/login/login",
      success(){
        wx.Utils.toast("退出登录成功！")
      }
    })
  }else{
    wx.Utils.toast("退出登录失败！")
  }
  
})
}
  }
})